package cc.rico.tutorial.helloworld;

import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author
 * @Date 2019-05-25 17:18
 * @Version 1.0
 * @Description
 */
@Configuration
public class HelloWorldConfiguration {
    protected final String helloWorldQueueName = "hello.world.queue";

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
        connectionFactory.setHost("192.168.223.150");
        connectionFactory.setVirtualHost("/develop");
        connectionFactory.setUsername("dev");
        connectionFactory.setPassword("123456");
        return connectionFactory;
    }

    @Bean
    public AmqpAdmin amqpAdmin() {
        return new RabbitAdmin(connectionFactory());
    }

    @Bean
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate template = new RabbitTemplate(connectionFactory());

        //The routing key is set to the name of the queue by the broker for the default exchange.
        template.setRoutingKey(this.helloWorldQueueName);

        //Where we will synchronously receive messages from
        template.setDefaultReceiveQueue(this.helloWorldQueueName);

        return template;
    }

    @Bean
    public Queue helloWorldQueue() {
        // Every queue is bound to the default direct exchange
        return new Queue(this.helloWorldQueueName);
    }
}
